package br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.components;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;

import org.w3c.dom.Element;

import br.inf.ufrgs.br.usixml4cdc.rendering.UsiXMLLayoutManagerRenderer;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.RendererFactory;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;
import br.inf.ufrgs.br.usixml4cdc.rendering.javadesktop.utils.InformationParser;
import br.inf.ufrgs.usixml4cdc.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4cdc.exceptions.RenderingErrorException;
import org.w3c.dom.Node;

public class UsiXMLJavaDesktopFlowBoxRenderer extends UsiXMLLayoutManagerRenderer {

	public UsiXMLJavaDesktopFlowBoxRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
	}

	public UsiXMLLayoutManagerRenderer getNextRenderer() {
		return null;
	}

	public String getValidElementName() {
		return "flowBox";
	}

	public Container processRenderization(Element element, Container contentPane) 
		throws ParsingErrorException,RenderingErrorException {
		
		FlowLayout flow = new FlowLayout();
		InformationParser infoParser = new InformationParser();
                
                Element elem = (Element)element;
		
		String alignmentString = elem.getAttribute("alignment");
		int alignment = infoParser.parseFlowLayoutAlignment(alignmentString);
		flow.setAlignment(alignment);
		
		contentPane.setLayout(flow);
        
		int numChildren = elem.getChildNodes().getLength();
		for (int i = 0; i < numChildren; i++) {
                    if (elem.getChildNodes().item(i) instanceof Element){
                        Element child = (Element) elem.getChildNodes().item(i);

                        Component component = (Component) 
                                RendererFactory.getInstance().
                                        getFirstElementRenderer((UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer).renderElement(child);

                        contentPane.add(component);
                    }
                }
		
		return contentPane;
		
	}
	
	public boolean validateElement(Element element) {
		return true;
	}
}
